package com.sky.controller.user;

import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody AddressBook addressBook) {
        addressBookService.insert(addressBook);
        return Result.success();
    }

    /**
     * 查询地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> selectAll(){
        return addressBookService.selectAll();
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> selectDefaultAddresss(){
        return addressBookService.selectDefaultAddresss();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        return addressBookService.setDefault(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable Long id){
        return addressBookService.selectById(id);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteById(Long id){
    	return addressBookService.deleteById(id);
    }
}
